package org.example.rabbitmq.config;

import org.example.rabbitmq.server.route.strategy.RouteStrategy;
import org.example.rabbitmq.server.route.strategy.impl.MappingRouteStrategy;
import org.example.rabbitmq.server.route.strategy.impl.ModRouteStrategy;
import org.example.rabbitmq.server.route.strategy.impl.RoundRobinRouteStrategy;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties({MultiQueueProperties.class, LatencyProperties.class})
public class ExampleAutoConfig {
    @Bean
    public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
        return new RabbitAdmin(connectionFactory);
    }

    @Bean
    public DirectExchange dataSynchronizeExchange(MultiQueueProperties multiQueueProperties) {
        roundRobinRouteStrategy();
        return new DirectExchange(multiQueueProperties.getExchangeName(), true, false);
    }

    /**
     * 选择路由策略，该策略通过 <b>TenantId</b> 确定路由 <b>routingKey</b> ，默认提供了三种路由策略：<p>
     * 1. {@link MappingRouteStrategy} 通过配置确定路由<p>
     * 2. {@link ModRouteStrategy} 通过租户ID取模确定路由<p>
     * 3. {@link RoundRobinRouteStrategy} 通过轮询确定路由
     *
     * @return
     */
    @Bean
    public RouteStrategy roundRobinRouteStrategy() {
        // MappingRouteStrategy, ModRouteStrategy
        return new RoundRobinRouteStrategy();
    }
}